package org.dromara.common.core.domain.model

import org.dromara.common.core.domain.dto.PostDTO
import org.dromara.common.core.domain.dto.RoleDTO
import java.io.Serial
import java.io.Serializable

/**
 * 登录用户身份权限
 *
 *@author LikeYouDo
 */
open class LoginUser(
    /**
     * 租户ID
     */
    var tenantId: String? = null,

    /**
     * 用户ID
     */
    var userId: Long? = null,

    /**
     * 部门ID
     */
    var deptId: Long? = null,

    /**
     * 部门类别编码
     */
    var deptCategory: String? = null,

    /**
     * 部门名
     */
    var deptName: String? = null,

    /**
     * 用户唯一标识
     */
    var token: String? = null,

    /**
     * 用户类型
     */
    var userType: String? = null,

    /**
     * 登录时间
     */
    var loginTime: Long? = null,

    /**
     * 过期时间
     */
    var expireTime: Long? = null,

    /**
     * 登录IP地址
     */
    var ipaddr: String? = null,

    /**
     * 登录地点
     */
    var loginLocation: String? = null,

    /**
     * 浏览器类型
     */
    var browser: String? = null,

    /**
     * 操作系统
     */
    var os: String? = null,

    /**
     * 菜单权限
     */
    var menuPermission: Set<String>? = null,

    /**
     * 角色权限
     */
    var rolePermission: Set<String>? = null,

    /**
     * 用户名
     */
    var username: String? = null,

    /**
     * 用户昵称
     */
    var nickname: String? = null,

    /**
     * 角色对象
     */
    var roles: List<RoleDTO>? = null,

    /**
     * 岗位对象
     */
    var posts: List<PostDTO>? = null,

    /**
     * 数据权限 当前角色ID
     */
    var roleId: Long? = null,

    /**
     * 客户端
     */
    var clientKey: String? = null,

    /**
     * 设备类型
     */
    var deviceType: String? = null,
) : Serializable {
    companion object {
        @Serial
        private val serialVersionUID = 1L
    }

    /**
     * 获取登录id
     */
    fun getLoginId(): String {
        requireNotNull(userType) { "用户类型不能为空" }
        requireNotNull(userId) { "用户ID不能为空" }
        return "$userType:$userId"
    }

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (other !is LoginUser) return false

        if (tenantId != other.tenantId) return false
        if (userId != other.userId) return false
        if (deptId != other.deptId) return false
        if (deptCategory != other.deptCategory) return false
        if (deptName != other.deptName) return false
        if (token != other.token) return false
        if (userType != other.userType) return false
        if (loginTime != other.loginTime) return false
        if (expireTime != other.expireTime) return false
        if (ipaddr != other.ipaddr) return false
        if (loginLocation != other.loginLocation) return false
        if (browser != other.browser) return false
        if (os != other.os) return false
        if (menuPermission != other.menuPermission) return false
        if (rolePermission != other.rolePermission) return false
        if (username != other.username) return false
        if (nickname != other.nickname) return false
        if (roles != other.roles) return false
        if (roleId != other.roleId) return false
        if (clientKey != other.clientKey) return false
        if (deviceType != other.deviceType) return false

        return true
    }

    override fun hashCode(): Int {
        var result = tenantId?.hashCode() ?: 0
        result = 31 * result + (userId?.hashCode() ?: 0)
        result = 31 * result + (deptId?.hashCode() ?: 0)
        result = 31 * result + (deptCategory?.hashCode() ?: 0)
        result = 31 * result + (deptName?.hashCode() ?: 0)
        result = 31 * result + (token?.hashCode() ?: 0)
        result = 31 * result + (userType?.hashCode() ?: 0)
        result = 31 * result + (loginTime?.hashCode() ?: 0)
        result = 31 * result + (expireTime?.hashCode() ?: 0)
        result = 31 * result + (ipaddr?.hashCode() ?: 0)
        result = 31 * result + (loginLocation?.hashCode() ?: 0)
        result = 31 * result + (browser?.hashCode() ?: 0)
        result = 31 * result + (os?.hashCode() ?: 0)
        result = 31 * result + (menuPermission?.hashCode() ?: 0)
        result = 31 * result + (rolePermission?.hashCode() ?: 0)
        result = 31 * result + (username?.hashCode() ?: 0)
        result = 31 * result + (nickname?.hashCode() ?: 0)
        result = 31 * result + (roles?.hashCode() ?: 0)
        result = 31 * result + (roleId?.hashCode() ?: 0)
        result = 31 * result + (clientKey?.hashCode() ?: 0)
        result = 31 * result + (deviceType?.hashCode() ?: 0)
        return result
    }


}
